//
//  JinRiMagazineViewController.m
//  JianChaRiBao
//
//  Created by 常兴宇 on 13-11-7.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "JinRiMagazineViewController.h"
#import "JinRiMagazineCell.h"
#import "MagazineInfoViewController.h"
#import "AppDelegate.h"
@interface JinRiMagazineViewController ()

@property (nonatomic, retain) NSArray * jinRiMagazineArray;
@end

@implementation JinRiMagazineViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bookshelf_cell_bg"]];
    jinRiMagazineGridView.backgroundView = bgView;
    jinRiMagazineGridView.leftContentInset = 20;
    jinRiMagazineGridView.rightContentInset = 20;


    [self getData];
}
-(void)getData{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id obj = [[DataRequest sharedRequest] getHistoryInfo:NEWSPAPERID];
        if ([obj isKindOfClass:[NSDictionary class]] && [[obj objectForKey:@"result"] isKindOfClass:[NSArray class]]) {
            self.jinRiMagazineArray = [NSArray arrayWithArray:[obj objectForKey:@"result"]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [jinRiMagazineGridView reloadData];
        });
    });

}

-(NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView{
    return self.jinRiMagazineArray.count;
    
}
-(AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index{
    static NSString *CellIdentify = @"ListenCell";
    JinRiMagazineCell * cell = (JinRiMagazineCell *)[gridView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[JinRiMagazineCell alloc]initWithFrame:CGRectMake(0, 0, 70, 100) reuseIdentifier:CellIdentify];
    }
    cell.jinRiMagazineInfo = [self.jinRiMagazineArray objectAtIndex:index];
    [cell updateDisplayWithImageStr:@"image"];
    
    return cell;
    
}
-(CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView{
    return CGSizeMake(80, 150);
}
- (CGRect)gridView:(AQGridView *)gridView adjustCellFrame:(CGRect)cellFrame withinGridCellFrame:(CGRect)gridCellFrame
{
    cellFrame.origin.y -= 5;
    return cellFrame;
}

-(void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index{
    
    MagazineInfoViewController * magazineInfoViewController = [[[MagazineInfoViewController alloc]initWithNibName:@"MagazineInfoViewController" bundle:nil]autorelease];
    magazineInfoViewController.basicInfo = [_jinRiMagazineArray objectAtIndex:index];
    [[UIApplication rootNavController] pushViewController:magazineInfoViewController animated:YES];
    [jinRiMagazineGridView deselectItemAtIndex:index animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [jinRiMagazineGridView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [jinRiMagazineGridView release];
    jinRiMagazineGridView = nil;
    [super viewDidUnload];
}
@end
